package com.yuanlrc.base.service.admin;

import com.alibaba.fastjson.JSON;
import com.yuanlrc.base.dao.admin.BorrowDao;
import com.yuanlrc.base.entity.admin.BookNameCount;
import com.yuanlrc.base.entity.admin.BorrowAmount;
import com.yuanlrc.base.entity.admin.StudentNameCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * 查询用户、图书借阅后台操作service
 */
@Service
public class QuantityService {
    @Autowired
    private BorrowDao borrowDao;
    /**
     * 查询借阅图书借阅排行
     * @param
     * @return list
     */
    public List<BookNameCount> findBookNameCount() {
        List<Object> bookNameCountList = borrowDao.findBookNameCount();
        BookNameCount bookcount = new BookNameCount();
        //存的是图书的名称
        List<String> name = new ArrayList<>();
        //存的是图书的数量
        List<Integer> cut = new ArrayList<>();
        List<BookNameCount> findCount = new ArrayList<>();
        //foreach遍历集合数据
        for (Object o : bookNameCountList) {
            Object[] group = (Object[]) o;
            //将bookName、bookCount的值添加到对应数组
            name.add(String.valueOf(group[0].toString()));
            cut.add(Integer.valueOf(group[1].toString()));
        }
        bookcount.setBookName(name);
        bookcount.setBookCount(cut);
        JSON.toJSONString(bookcount);
        List<BookNameCount> list= new ArrayList<>();
        list.add(bookcount);
        return list;

    }
    /**
     * 查询用户借阅图书排行
     * @param
     * @return list
     */
    public List<StudentNameCount>findStudentNameCount() {
        List<Object> studentNameCountList = borrowDao.findStudentNameCount();
        StudentNameCount stucount = new StudentNameCount();
        List<String> name = new ArrayList<>();
        List<Integer> cut = new ArrayList<>();
        List<StudentNameCount> findCount = new ArrayList<>();
      //foreach遍历集合数据
        for (Object o : studentNameCountList) {
            Object[] group = (Object[]) o;
            //将studentName、studentCount的值添加到对应数组
            name.add(String.valueOf(group[0].toString()));
            cut.add(Integer.valueOf(group[1].toString()));
        }
        stucount.setStudentName(name);
        stucount.setStudentCount(cut);
        JSON.toJSONString(stucount);
        List<StudentNameCount> list= new ArrayList<>();
        list.add(stucount);
        return list;

    }
    /**
     * 查询用户借阅图书数量排行
     * @param
     * @return list
     */
    public List<BorrowAmount> findStudentBorrowNum(){

        List<Object> studentBorrowNumList = borrowDao.findStudentBorrowNum();
        BorrowAmount amount = new BorrowAmount();
        List<String> name = new ArrayList<>();
        List<Integer> cut = new ArrayList<>();
        List<StudentNameCount> findCount = new ArrayList<>();
        //foreach遍历集合数据
        for (Object o : studentBorrowNumList) {
            Object[] group = (Object[]) o;
            //将studentName、studentCount的值添加到对应数组
            name.add(String.valueOf(group[0].toString()));
            cut.add(Integer.valueOf(group[1].toString()));
        }
        amount.setStudentName(name);
        amount.setBorrowNum(cut);
        JSON.toJSONString(amount);
        List<BorrowAmount> list= new ArrayList<>();
        list.add(amount);
        return list;

    }
}
